package com.forum.controller;

import com.forum.jspmine.common.reposresult.PageResult;
import com.forum.jspmine.common.reposresult.Result;
import com.forum.jspmine.common.reposresult.StatusCode;
import com.forum.pojo.Label;
import com.forum.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:jayChou
 * @date:2019/7/19 description:神来之手
 * 动态获取菜单
 */

@RestController
@RequestMapping("/label")
public class LableController {

    @Autowired
    private LabelService labelService;

    @GetMapping
    public Result findAll(){
        List<Label> list = labelService.findAll();

        return new Result(StatusCode.OK,true,"查询成功",list);

    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){

        Label label = labelService.findById(id);

        return new Result(StatusCode.OK,true,"查询成功",label);

    }

    @PostMapping
    public Result save(@RequestBody Label label){

        labelService.add(label);

        return new Result(StatusCode.OK,true,"添加成功");

    }

    @PutMapping("/{id}")
    public Result update(@PathVariable String id ,@RequestBody Label label){
        label.setId(id);
        labelService.update(label);

        return new Result(StatusCode.OK,true,"修改成功");

    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){

        labelService.delete(id);

        return new Result(StatusCode.OK,true,"删除");
    }

    /**
    *@author:jayChou
    *@date:2019/9/12
    *description:匹配查询
    *
    */


    @PostMapping("/search")
    public Result search(@RequestBody Label label){

        List<Label> list=labelService.search(label);
        return new Result(StatusCode.OK,true,"查询成功",list);



    }


    @PostMapping("/search/{page}/{size}")
    public Result searchByPage(@PathVariable Integer page,
                               @PathVariable Integer size,
                               @RequestBody Label label){

        Page<Label> labels=labelService.search(page,size,label);

        return new Result(StatusCode.OK,true,
                "查询成功",new PageResult<Label>(
                (long) labels.getTotalPages(),labels.getContent()));


    }


}
